package cn.ms.doctor.Resolver;

import cn.ms.doctor.Entity.User;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Objects;

public class GetSessionUserResolver implements HandlerMethodArgumentResolver {
    /**
     * 自定义解析器解析类型
     * @param parameter
     * @return
     */
    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        return parameter.getParameterType().isAssignableFrom(User.class)&&parameter.hasParameterAnnotation(GetSessionUserInter.class);
    }

    /**
     * 获取到参数后返回数据
     * @param parameter
     * @param mavContainer
     * @param webRequest
     * @param binderFactory
     * @return
     * @throws Exception
     */
    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
        HttpSession session = Objects.requireNonNull(webRequest.getNativeRequest(HttpServletRequest.class)).getSession();
        System.out.println(session.getId());
        return session.getAttribute("userMsg");
    }
}
